<?php


namespace App\Services;

use App\Models\Course;
use App\Models\PracticalTraining;
use App\Models\PracticalTrainingUser;

class PracticalTrainingService extends BaseService
{
    /**
     * 获取实训列表
     * @return
     */
    public function list($page,$limit,$sort,$order, $name,$course_id) {
        $query = PracticalTraining::leftJoin('courses', 'courses.id', '=', 'practical_trainings.course_id')
            ->select('practical_trainings.*','courses.name as course_name');
        if(!empty($name)){
            $query->where('practical_trainings.name','like',"%$name%");
        }
        if(isset($course_id)){
            $query->where('course_id',$course_id);
        }

        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }
    /**
     * 获取用户实训列表
     * @return
     */
    public function userPracticalList($page,$limit,$sort,$order, $name,$course_id,$users_id) {
        $query = PracticalTrainingUser::leftJoin('practical_trainings', 'practical_trainings.id', '=', 'practical_training_users.practical_training_id')
            ->leftJoin('courses', 'courses.id', '=', 'practical_trainings.course_id')
            ->select('practical_trainings.*','courses.name as course_name');

        if(!empty($name)){
            $query->where('practical_trainings.name','like',"%$name%");
        }
        if(isset($course_id)){
            $query->where('course_id',$course_id);
        }
        if(isset($users_id)){
            $query->where('users_id',$users_id);
        }
        return $query->distinct()->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }

    /**
     * 获取实训信息
     * @return
     */
    public function getByPracticalTrainingInfo($id) {
        $query = PracticalTraining::leftJoin('courses', 'courses.id', '=', 'practical_trainings.course_id')
            ->select('practical_trainings.*','courses.name as course_name');
        return $query->where('practical_trainings.id',$id)->get();
    }
    /**
     * 获取实训信息
     * @return
     */
    public function getByTrainingTime($id) {
        return PracticalTraining::query()->where('id',$id)->first('ent_time');
    }

}
